package com.qf.sy2103.homework01.service;

import com.qf.sy2103.homework01.dao.BookDao;
import com.qf.sy2103.homework01.entity.Book;
import com.qf.sy2103.homework01.utils.FileUtils;
import com.qf.sy2103.homework01.utils.MybatisUtil;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class DataImportService {

    public static final int BatchCount = 5000;

    public static void CunFile(String file) throws IOException {
        BookDao mapper = MybatisUtil.getMapper(BookDao.class);
        int count = Math.toIntExact(mapper.count());
        int num=500;
        int yema=count/num+1;
        List<String> list=new ArrayList<>();
        for (int i=1;i<=yema;i++){
            final BookDao bookDao = MybatisUtil.getMapper(BookDao.class);
            List<Book> list1 = bookDao.selectBook(((i - 1) * num), num);
            for (Book book:list1){
                list.add(book.getName()+","+book.getAuthor()+","+book.getPublish()+","+book.getSort());
            }

           FileUtils.writeListToFile(list,file);
            list.clear();
        }
    }

    /**
     * 把指定的文本文件中的数据导入数据库表中
     *
     * @param relativePath
     */
    public static void importData(String relativePath) throws IOException {

        //1. 把relativePath转换成输入流对象
        final BufferedReader bufferedReader = FileUtils.getBufferedReader(relativePath);

        //2. 分批处理数据 ，每次从输入流中读取1000条数据，然后把1000条数据写如 数据库表 t_books中。
        ArrayList<String> list = new ArrayList<>();
        String line = null;

        int count = 1;
        while ((line = bufferedReader.readLine()) != null) {
            list.add(line);
            if (list.size() == BatchCount) {  // 如果当前list中的条数不足BatchCount，则继续循环
                System.out.println(count++);
                writeListToDB(list);  // 每BatchCount条数据 进行一次写入数据库表的操作
                list = new ArrayList<String>();  // 注意，每次写完之后重新创建一个新的list用来存放下一批数据
            }
        }
        if (list.size()>0){
            System.out.println("不足5000条，还剩"+list.size());
            writeListToDB(list);  // 每BatchCount条数据 进行一次写入数据库表的操作
            list = new ArrayList<String>();  // 注意，每次写完之后重新创建一个新的list用来存放下一批数据
        }

        //3. 关闭流对象
        bufferedReader.close();

    }

    /**
     * 把list中的数据写入数据库表中
     *
     * @param list
     */
    private static void writeListToDB(ArrayList<String> list) {
        //1. 把list转换成 List<Book>
        final ArrayList<Book> books = new ArrayList<>();
        list.forEach(s -> {
            final String[] arrs = s.split(","); //  [JustForFun,linusTorwards,Oreeelly,100]
            final Book book = new Book();
            book.setName(arrs[0]);
            book.setAuthor(arrs[1]);
            book.setPublish(arrs[2]);
            book.setSort(arrs[3]);
            books.add(book);
        });

        //2. 利用封装好的Mybatis的工具类获得BookDao接口的对象，调用批量插入方法
        final BookDao bookDao = MybatisUtil.getMapper(BookDao.class);
        bookDao.insertBooks(books);
        MybatisUtil.commit();


    }


}
